/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Interfata;

import Arbori.Nod;
import Visitor.ToXMLVisitor;
import XML.DocumentXML;
import XML.TagXML;
import XML.ToFileVisitor;

/**
 *
 * @author AlinT
 */
public class CreazaXML extends Complet implements Command {

    /**
     * Adauga nodul in fisierul XML , apoi tag-uri XML cu fii sai
     * @param n nodul ce va fi adaugat in fisier
     * @return scrie tag-ul XML al nodului 
     */
    public static TagXML createXMLTree(Nod n)
	{
	TagXML t1= null;
	TagXML t2 =null;
	if (n==null) return null;
        if (n.getfStang()!=null)
		{t1=createXMLTree(n.getfStang());}
	if (n.getfDrept()!=null)
		{t2=createXMLTree(n.getfDrept());}
	TagXML result=new TagXML(n.getClass().getName());
	if (t1!=null)result.addTag(t1);
	if (t2!=null)result.addTag(t2);
	return result;
    }

    /**
     * Metoda execute() corespunzatoare sablonului Command - creaza fisierul
     * ce contine arborele XML
     *
     */
    public void execute(){
        CreazaCopac copac=new CreazaCopac(GUInterfata.getFunctie());
        ToXMLVisitor o=new ToXMLVisitor();
        copac.arbore.acceptVisitor(o);
        TagXML t=createXMLTree(copac.arbore.getRadacina());
        System.out.println(copac.arbore.getRadacina());
        DocumentXML x=new DocumentXML(t, "Copac.XML");
	ToFileVisitor f=new ToFileVisitor();
	x.acceptVisitor(f);
          
    }
        
}
